home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Mac Game Programming Gurus / TricksOfTheMacGameProgrammingGurus.iso / More Source / C⁄C++ / DialogControls / MovableModal.c < prev    next >
C/C++ Source or Header  |  1995-05-09  |  5KB  |  196 lines

  1. #include "MovableModal.h"
  2. #include "MyMenus.h"
  3.  
  4. /* MovableModalDialog requires the following function(s) to exist:
  5.     FindWindowToUpdate( WindowPtr window ) - calls an update procedure for ALL possible windows
  6.         -- This function must exist in a file called UpdatesFunctions.h
  7.     DoThisMenu( long menu ) - calls the menu handling procedure in the main function
  8.         -- This function must exist in a file called MyMenusFunctions.h
  9.         
  10.    The reason this requires such a short file in this case (MyMenus.c) is that normally
  11.    there is a lot more that needs to be done with menus that isn't done here.
  12.    
  13.    Movable Modal Dialog requires the that you call these functions yourself:
  14.     TwiddleMenus() - Sets all menus so that they are hilited properly
  15.     MovableModalMenus() - Sets all menus so that they are dimmed properly
  16.     
  17.     I got most of this code from someone, and don't remember who.  If this looks really
  18.     familiar, and you've put your code out in PD, tell me and I'll give you credit here.
  19. */
  20.  
  21. void DoThisMenu( long menuCode );
  22. void FindWindowToUpdate( WindowPtr window );
  23.  
  24.  
  25. /** The MovableModal dialog routine.  I don't really know why I include the whichDlog item,
  26.     but it's there.  You really should just use FrontWindow() to determine which window
  27.     to make modal, but I included this.  **/    
  28.  
  29. void    Nothing( void );
  30. void    Nothing( void )
  31. {}
  32.  
  33. void    MovableModalDialog( ModalFilterUPP    filterProc, short *itemHit, DialogPtr whichDlog )
  34. {
  35.     EventRecord        theEvent;
  36.         
  37.     Boolean        gotEvent, eventHandled;
  38.     OSErr        myErr;
  39.      ModalFilterUPP theModalProc;
  40.     
  41.     gotEvent = false;
  42.     
  43.     *itemHit = 0;
  44.  
  45.     HiliteMenu(0);
  46.  
  47.     while (!gotEvent)
  48.     {
  49.         eventHandled = false;
  50.  
  51.         gotEvent = WaitNextEvent(everyEvent, &theEvent, 30, nil);
  52.         
  53.         if( IsDialogEvent( &theEvent ) ) 
  54.         {
  55.             if ( filterProc )
  56.             {                
  57.                 if ( !(eventHandled = CallModalFilterProc( filterProc, whichDlog, &theEvent, itemHit )) )
  58.                 {
  59.                     myErr = GetStdFilterProc(&theModalProc);
  60.                     if (myErr == noErr)
  61.                         eventHandled = StdFilterProc(whichDlog, &theEvent, itemHit);
  62.  
  63.                     if( !eventHandled )
  64.                         eventHandled = DialogSelect( &theEvent, &whichDlog, itemHit );
  65.                 }
  66.             }
  67.             else
  68.             {
  69.                 myErr = GetStdFilterProc(&theModalProc);
  70.                 if (myErr == noErr)
  71.                     eventHandled = StdFilterProc(whichDlog, &theEvent, itemHit);
  72.                 
  73.  
  74.                 if( !eventHandled )
  75.                     eventHandled = DialogSelect( &theEvent, &whichDlog, itemHit );
  76.             }
  77.             
  78.         }
  79.         else
  80.         {
  81.             if ( filterProc )
  82.                 eventHandled = CallModalFilterProc( filterProc, whichDlog, &theEvent, itemHit );
  83.         }
  84.  
  85.     if( theEvent.what == activateEvt )
  86.     {
  87.         SetCursor(&qd.arrow);
  88.         if ((theEvent.modifiers & activeFlag ) != 0)
  89.             DoMyActivate( whichDlog );
  90.         else
  91.             DoMyDeactivate( whichDlog );
  92.     }
  93.  
  94.         if (!eventHandled)
  95.         {
  96.             switch ( theEvent.what )
  97.             {
  98.                 case mouseDown:
  99.                     {
  100.                     WindowPtr    theWindow;
  101.                     long        menuCode;
  102.                     short        part = FindWindow(theEvent.where, &theWindow);
  103.         
  104.                         switch (part)
  105.                         {
  106.                             case inDrag:
  107.                                 if (theWindow==whichDlog)
  108.                                 {
  109.                                     DragWindow(theWindow, theEvent.where, 
  110.                                                 &qd.screenBits.bounds);    
  111.                                 }
  112.                                 else
  113.                                     SysBeep(20);
  114.                                 break;
  115.                             case inSysWindow:
  116.                                 SystemClick(&theEvent, theWindow);
  117.                                 break;
  118.                             case inMenuBar:
  119.                                 menuCode = MenuSelect(theEvent.where);
  120.                                 DoThisMenu( menuCode );
  121.                                 break;
  122.                             case inContent:
  123.                                 if (theWindow != whichDlog)
  124.                                     SysBeep(20);
  125.                                 break;
  126.                             default:
  127.                                 SysBeep(20);
  128.                                 break;
  129.                         }
  130.                     }
  131.                     break;
  132.                 case keyDown:
  133.                 {
  134.                     long menu;
  135.                     if(( theEvent.modifiers & cmdKey ) != 0 )
  136.                     {
  137.                         menu = MenuKey( theEvent.message & charCodeMask );
  138.                         DoThisMenu( menu );
  139.                     }
  140.                 }
  141.                     break;
  142.                 
  143.                 case updateEvt:
  144.                     FindWindowToUpdate( (WindowPtr) theEvent.message );
  145.                     break;
  146.                 case nullEvent:
  147.                     break;
  148.             }    
  149.         }
  150.     }
  151.     
  152.     HiliteMenu( 0 );
  153. }
  154.  
  155.  
  156. /** When a window is sent to the background, it should disable all of it's controls
  157.     This function does that.  **/
  158. void    DoMyDeactivate( WindowPtr theWindow )
  159. {
  160.     ControlHandle    theList;
  161.     GrafPtr            thePort;
  162.     
  163.     GetPort( &thePort );
  164.     SetPort( theWindow );
  165.     
  166.     theList = (*(WindowPeek)theWindow).controlList;
  167.     
  168.     while( theList != nil )
  169.     {
  170.         HiliteControl( theList, 255 );
  171.         theList = (**theList).nextControl;
  172.     }
  173.     
  174.     SetPort( thePort );
  175. }
  176.  
  177. /** When a window is activated, it should reactivate all of it's controls.
  178.     This function does that. **/
  179. void    DoMyActivate( WindowPtr theWindow )
  180. {
  181.     ControlHandle    theList;
  182.     GrafPtr            thePort;
  183.     
  184.     GetPort( &thePort );
  185.     SetPort( theWindow );
  186.     
  187.     theList = (*(WindowPeek)theWindow).controlList;
  188.     
  189.     while( theList != nil )
  190.     {
  191.         HiliteControl( theList, 0 );
  192.         theList = (**theList).nextControl;
  193.     }
  194.     
  195.     SetPort( thePort );
  196. }